home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / pctj8411.arc / STACK.MOD < prev    next >
Text File  |  1986-09-14  |  1KB  |  45 lines

  1. MODULE StackIllustration;
  2. (* 
  3.    This program module illustrates how the services of the library 
  4.    module StackHandler can be used to implement a very simple stack-based
  5.    adding machine. Note how we can declare a variable, S, of type
  6.    Stack without knowing how stacks are actually implemented (arrays,
  7.    linked lists, etc.). We only care to know that the procedures Push,
  8.    Pop, etc. work as claimed. This module also imports I/O services
  9.    from the standard library module InOut.
  10. *)
  11. FROM InOut IMPORT Read, WriteString, WriteLn, ReadInt, WriteInt;
  12. FROM StackHandler IMPORT Stack, Initialize, Push, Pop, Empty;
  13. VAR
  14.  S : Stack;
  15.  i : INTEGER;
  16.  ch : CHAR;
  17. BEGIN
  18.  (* Initialize stack for further work. *)
  19.  Initialize(S);
  20.  
  21.  (* Collect input data, push onto stack. *)
  22.  REPEAT
  23.    WriteString("Enter: ");
  24.    ReadInt(i);
  25.    WriteLn;
  26.    Push(i, S);
  27.    WriteString("Done?");
  28.    Read(ch);
  29.    WriteLn;
  30.  UNTIL (ch = "Y");
  31.  
  32.  (* Initialize i then accumulate sum of stack elements. *)
  33.  i := 0;
  34.  WHILE NOT Empty(S) DO
  35.    i := i + Pop(S);
  36.  END;
  37.  
  38.  (* Write sum in a field 7 digits wide. *)
  39.  WriteString("Sum= ");
  40.  WriteInt(i, 7);
  41.  WriteLn; 
  42.  
  43. END StackIllustration.
  44.  
  45.